<?php
/**
 * Created by PhpStorm.
 * User: 73640
 * Date: 2018/11/22
 * Time: 11:24
 */

namespace App\Repository;
//分享的逻辑在此

use App\Exceptions\ShareException;
use App\model\broker\BrokerCustomer;
use App\model\broker\BrokerProject;
use App\model\broker\BrokerRewardRecord;
use App\model\BrokerRecords;
use App\User;
use Illuminate\Support\Carbon;

class ShareRepository
{

    public function bindUser(User $fromUser, User $toUser,BrokerProject $project){//分享user
        //检查formUser是否已经绑定本项目的其他人了
        $hasBrokerCustomer=BrokerCustomer::where('project_id',$project->id)
            ->where('user_id',$fromUser->id)->first();
        if ($hasBrokerCustomer)
            return true;
        //同一人则不绑定
        if($fromUser->id == $toUser->id)
            return true;
        //查询本项目直推的上级
       $toUserCustomer= BrokerCustomer::where('user_id',$toUser->id)
            ->where('project_id',$project->id)
            ->first();
        //绑定操作
       $brokerCustomer= new BrokerCustomer();
       $brokerCustomer->user_id=$fromUser->id;
       $brokerCustomer->first_id=$toUser->id;
       $brokerCustomer->second_id=$toUserCustomer->first_id ?? '';
        $brokerCustomer->project_id=$project->id;
       $brokerCustomer->effective_first_reward=$project->effective_first_reward;
       $brokerCustomer->effective_share_reward=$project->effective_share_reward;
       $brokerCustomer->follow_first_reward=$project->follow_first_reward;
       $brokerCustomer->follow_share_reward=$project->follow_share_reward;
       $brokerCustomer->transaction_first_reward=$project->transaction_first_reward;
       $brokerCustomer->transaction_share_reward=$project->transaction_share_reward;
       $brokerCustomer->status=BrokerCustomer::REVIEW;//待审核
       $brokerCustomer->second_reward=$project->second_reward;
       $brokerCustomer->save();
       //发送短信
        //:todo 发送短信
    }

    /**
     * @param BrokerCustomer $brokerCustomer
     * @param $status
     */
    public function changeStatus(BrokerCustomer $brokerCustomer,$status){
        //dd(isset(BrokerCustomer::$statusMap[$status]));
        if(!isset(BrokerCustomer::$statusMap[$status])){
            throw new ShareException('类型不存在');
        }
        switch ($status){

            case BrokerCustomer::INVALiD:
                return $this->INVALiD($brokerCustomer);
                break;
            case  BrokerCustomer::FOLLOW:
                return $this->FOLLOW($brokerCustomer);
                break;
            case  BrokerCustomer::TRANSACTION:
                $this->TRANSACTION($brokerCustomer);
                break;
            case  BrokerCustomer::UNINTENTIONAL:
                $this->UNINTENTIONAL($brokerCustomer);
                break;
            case  BrokerCustomer::EFFECTIVE;
                $this->EFFECTIVE($brokerCustomer);
                break;
        }
    }
    protected  function  EFFECTIVE(BrokerCustomer $brokerCustomer){//有效中跟进
        if($brokerCustomer->status==BrokerCustomer::REVIEW){
            \DB::transaction(function()use($brokerCustomer){
                //$brokerCustomer->status=BrokerCustomer::EFFECTIVE;
                //$brokerCustomer->effective_time=Carbon::now();
                //$brokerCustomer->save();
                //直推奖励
                if($brokerCustomer->first_id){
                    $this->ShareReward($brokerCustomer->user_id,$brokerCustomer->first_id,$brokerCustomer->effective_first_reward,BrokerRewardRecord::effective_direct);
                }
                //上级奖励
                if ($brokerCustomer->second_id){
                    $this->ShareReward($brokerCustomer->user_id,$brokerCustomer->second_id,$brokerCustomer->effective_first_reward*((int)($brokerCustomer->second_reward/100)));
                }
            });
        }else{
            throw  new ShareException('状态不是待审核状态');
        }
    }
    protected  function INVALiD(BrokerCustomer $brokerCustomer){//无效
        if($brokerCustomer->status==BrokerCustomer::REVIEW){
            //$brokerCustomer->status=BrokerCustomer::INVALiD;
            //$brokerCustomer->save();
        }else{
            throw  new ShareException('状态不是待审核状态');
        }
    }
    protected  function FOLLOW(BrokerCustomer $brokerCustomer){//
        if($brokerCustomer->status==BrokerCustomer::EFFECTIVE){
            \DB::transaction(function()use($brokerCustomer){
                //$brokerCustomer->status=BrokerCustomer::FOLLOW;
                //$brokerCustomer->follow_time=Carbon::now();
                //$brokerCustomer->save();
                //直推奖励
                if($brokerCustomer->first_id){
                    $this->ShareReward($brokerCustomer->user_id,$brokerCustomer->first_id,$brokerCustomer->follow_first_reward,BrokerRewardRecord::follow_direct);
                }
                //上级奖励
                if ($brokerCustomer->second_id){
                    $this->ShareReward($brokerCustomer->user_id,$brokerCustomer->second_id,$brokerCustomer->follow_first_reward*((int)($brokerCustomer->second_reward/100)));
                }
            });
            }else{
            throw  new ShareException('状态不是有效');
        }
    }
    protected  function TRANSACTION(BrokerCustomer $brokerCustomer){
        if($brokerCustomer->status==BrokerCustomer::FOLLOW){
            \DB::transaction(function()use($brokerCustomer){
                //$brokerCustomer->status=BrokerCustomer::TRANSACTION;
                //$brokerCustomer->transaction_time=Carbon::now();
                //$brokerCustomer->save();
                //直推奖励
                if($brokerCustomer->first_id){
                    $this->ShareReward($brokerCustomer->user_id,$brokerCustomer->first_id,$brokerCustomer->transaction_first_reward,BrokerRewardRecord::transaction_direct);
                }
                //上级奖励
                if ($brokerCustomer->second_id){
                    $this->ShareReward($brokerCustomer->user_id,$brokerCustomer->second_id,$brokerCustomer->transaction_first_reward*((int)($brokerCustomer->second_reward/100)));
                }
            });
        }else{
            throw  new ShareException('状态不是有效');
        }
    }

    protected  function UNINTENTIONAL(BrokerCustomer $brokerCustomer){
        if($brokerCustomer->status==BrokerCustomer::FOLLOW){
            //$brokerCustomer->status=BrokerCustomer::UNINTENTIONAL;
            //$brokerCustomer->save();
        }else{
            throw  new ShareException('状态不是继续跟进');
        }
    }

    /**
     * @param $user_id
     * @param $reward
     * @param $reward_type
     */
    public function ShareReward($from_user_id,$to_user_id,$reward,$reward_type){//发送奖励
        $BrokerReward= new BrokerRewardRecord();
        $BrokerReward->from_user_id=$from_user_id;
        $BrokerReward->to_user_id=$to_user_id;
        $BrokerReward->reward=$reward;
        $BrokerReward->reward_type=$reward_type;
        $BrokerReward->save();
        User::find($to_user_id)->increment('reward',$reward);
        //短信发送
    }

}